﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using System.Diagnostics;
using Microsoft.WindowsCE.Forms;
namespace TTBox
{
    
        
    public partial class LoginPanel : UserControl
    {
        public LoginPanel()
        {
            InitializeComponent();

            this.timer_RunChecker.Enabled = true;


        }
        public void initial()
        {
            Form_Main.Main.m_bLogined = false;

            CmdTalker.getInstance().SendServer("6000");

        }
        bool m_bShowRE = false;
        private void button_Login_1_Click(object sender, EventArgs e)
        {
            Form_Main.Main.resetBackLightTimeout();
            string str = ((Button)sender).Text;

            int n = this.textBox_LoginID.Text.IndexOf("＿");
            if (n != -1)
            {
                this.textBox_LoginID.Text = this.textBox_LoginID.Text.Substring(0, n) + str + this.textBox_LoginID.Text.Substring(n + 1);
            }

            if (this.textBox_LoginID.Text == "９８９８" && str == "０")
            {
                Form_Main.Main.button_Close_Click(null, null);
                return;
            }

            if (this.textBox_LoginID.Text == "９９９８" && str == "０")
            {
                Form_Main.Main.button_Log_Click(null, null);
                return;
            }


            if (this.textBox_LoginID.Text == "９１９１" && str == "０")
            {
                Form_Main.Main.SetTopMost(false);
                Form_Main.Main.SendToBack();
                

                //throw new Exception("Test Error...");
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo = new System.Diagnostics.ProcessStartInfo("\\FlashDisk\\PNDsoftware1\\Navi\\Navigator.exe", "");
                p.Start();
              
            }
            if (this.textBox_LoginID.Text == "９１９２" && str == "０")
            {
                /*
                Message msg = Message.Create(Form_Main.Main.Handle, MsgWindow.REMSG_SETTOP, new IntPtr(0), new IntPtr(0));
                MessageWindow.SendMessage(ref msg);
                LogManager.getInstance().Log("SndMsg:" + Form_Main.Main.Handle.ToString() + ":" + MsgWindow.REMSG_SETTOP);
                 * */

                LogManager.getInstance().Log("TEST ExceP", new Exception("Test Excep"));
            }



            if (this.textBox_LoginID.Text == "９９９２" && str == "０")
            {

                Form_Main.Main.resetBackLightTimeout();
                Form_Main.Main.SetTopMost(true);

                Form_Main.Main.m_bLogined = false;
                Form_Main.Main.ChangeToMainScreen();
                Form_Main.Main.SetCurrStatus(TaxiStatus.Free);

            }
            if (this.textBox_LoginID.Text == "９９９３" && str == "０")
            {


 
                /*                
                char chVoice[100] = ....;
                 
                COPYDATASTRUCT cds;
                cds.dwData = ePlayVoice;
                cds.cbData = 100 * sizeof(char);
                cds.lpData = chVoice;
                 
                SendMessage(hWnd, WM_COPYDATA, &cds);
                */ 
            }


        }

        private void button_Login_Clear_Click(object sender, EventArgs e)
        {
            Form_Main.Main.resetBackLightTimeout(); 
   
            
            this.textBox_LoginID.Text = "＿＿＿＿";

        }
        string strFull = "０１２３４５６７８９＿";
        string strHalf =  "0123456789_" ;
        private void button_Login_Login_Click(object sender, EventArgs e)
        {
            Form_Main.Main.resetBackLightTimeout();
            //CmdTalker.getInstance().initial("COM9");
            this.timer_RunChecker.Enabled = false;
            string strResult = "";
            if (this.textBox_LoginID.Text.Length == 4)
            {
                for (int i = 0; i < 4; i++)
                {
                    int n = strFull.IndexOf(this.textBox_LoginID.Text[i]);
                    strResult += strHalf[n];
                }



                string strTemp = "";

                Form_Main.Main.ShowWaitingPanel();
                //CmdTalker.getInstance().SendServerAndWaitResponse("669" + strResult, 15, "%A");

                CmdTalker.getInstance().SendServer("669" + strResult);
                DateTime dtWait = System.DateTime.Now.Add(new TimeSpan(0, 0, 10));
                strTemp = CmdTalker.getInstance().checkServerCMD("%A");
                while (System.DateTime.Now < dtWait && strTemp == "")
                {
                    System.Threading.Thread.Sleep(100);
                    strTemp = CmdTalker.getInstance().checkServerCMD("%A");
                    Application.DoEvents();
                }

                Form_Main.Main.HideWaitingPanel();
                if (strTemp == "%A0")
                {
                    this.label_LoginFail.Visible = true;
                    this.label_LoginFail.BringToFront();
                    this.label_LoginFail.Tag = 3;
                    this.Refresh();
                }
                else if (strTemp == "%A1")
                {
                    try
                    {
                        
                       // //System.Diagnostics.Process p = new System.Diagnostics.Process();
                       // p.StartInfo = new System.Diagnostics.ProcessStartInfo(Form_Main.Main.StartLoc + "..\\Navigator\\MobileNavigator.exe", "");
                       // p.Start();

                        SoundManager.getInstance().playWelcome();
                    }
                    catch (Exception ex)
                    {
                        LogManager.getInstance().Log("Navi Load Error..", ex);
                    }
                    finally
                    {
                        Form_Main.Main.SetTopMost(true);
                    }


                    Form_Main.Main.m_bLogined = true;
                    Form_Main.Main.ChangeToMainScreen();
                    Form_Main.Main.SetCurrStatus(TaxiStatus.Free);
                }
                else if(strTemp == "")
                {
                    this.label_LoginFail.Visible = true;
                    this.label_LoginFail.BringToFront();
                    this.label_LoginFail.Tag = 2;
                    this.Refresh();


                }
            }

            this.timer_RunChecker.Enabled = true;


        }

        private void timer_RunChecker_Tick(object sender, EventArgs e)
        {
            if (this.label_LoginFail.Visible)
            {
                SoundManager.getInstance().playMsgRecv();

                if ((int)this.label_LoginFail.Tag > 0)
                {
                    this.label_LoginFail.Tag = (int)this.label_LoginFail.Tag - 1;
                }
                else
                {
                    this.label_LoginFail.Visible = false;
                }
            }
            
            
            if (Form_Main.Main.getBoxStatus(BoxStatusItem.GPRS))
                this.button_Login_Login.Enabled = true;
            else
                this.button_Login_Login.Enabled = false;
            
            
        }

        private void textBox_LoginID_GotFocus(object sender, EventArgs e)
        {
            this.button_Login_0.Focus();
        }

        private void label_Ver_ParentChanged(object sender, EventArgs e)
        {

        }
    }
}
